package com.github.llyb120.calengine.parser;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;
import com.github.llyb120.calengine.rule.RuleEngine;
import com.github.llyb120.performance.define.Datasource;
import lombok.Data;
import lombok.Synchronized;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DatasourceParser extends AbstractParser {

    private static Map<String, SQLManager> sqlManagers = new ConcurrentHashMap<>();

    @Data
    static class DatasourceOption {
        @Option(name = "-use", usage = "使用哪个数据源")
        String use;

        @Option(name = "-sql", usage = "执行的sql")
        String sql;

        @Option(name = "-target", usage = "输出的结果集")
        String target;
    }

    @Override
    public void parse(String[] args) throws Exception {
        DatasourceOption option = new DatasourceOption();
        CmdLineParser parser = new CmdLineParser(option);
        parser.parseArgument(args);
        if (parser.getOptions().size() == 2) {
//            generate(option.src, option.target);
        } else {
            parser.printUsage(System.out);
        }
    }

    public List<JSONObject> test(String use, String sql, String target) {
//        RuleEngine ruleEngine = new RuleEngine();
//        Datasource datasource = ruleEngine.getRule(RuleEngine.DATASOURCE, use);
//        if (datasource == null) {
//            return new ArrayList<>();
//        }
//        List<JSONObject> result = getSqlManager(ruleEngine, use).execute(new SQLReady(sql), JSONObject.class);
//        ExcelWriter writer = ExcelUtil.getWriter(new File(target));
//        writer.autoSizeColumnAll();
//        writer.write(result, true);
//        writer.flush();
//        writer.close();
//        return result;
        return null;
    }




}
